package com.candy.test;

import com.candy.bean.ListDemo;
import com.candy.dao.AccountDao;
import com.candy.service.AccountService;
import com.candy.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Client {

    @Test
    public  void test08() {
//        1、获取容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans_di.xml");
//        通过bean的id进行对象获取
        ListDemo listDemo= (ListDemo) ac.getBean("listDemo");
        System.out.println(listDemo);

    }

    /**
     * 使用p名称空间进行注入测试
     */
    @Test
    public  void test07() {
//        1、获取容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans_di.xml");
//        通过bean的id进行对象获取
        AccountService accountService= (AccountService) ac.getBean("accountService3");
        System.out.println(accountService);
        accountService.save();
    }


    /**
     * 使用set方法进行注入测试
     */
    @Test
    public  void test06() {
//        1、获取容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans_di.xml");
//        通过bean的id进行对象获取
        AccountService accountService= (AccountService) ac.getBean("accountService2");
        System.out.println(accountService);
        accountService.save();
    }

    /**
     * 测试依赖注入
     */
    @Test
    public  void test05() {
//        1、获取容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans_di.xml");
//        通过bean的id进行对象获取
        AccountService accountService= (AccountService) ac.getBean("accountService");
        System.out.println(accountService);
    }



    /**
     * 测试实例工厂进行对象实例化的操作
     */
    @Test
    public  void test04() {
//        1、获取容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//        通过bean的id进行对象获取
        AccountService accountService= (AccountService) ac.getBean("accountService3");
        System.out.println(accountService);

    }
    /**
     * 测试静态工厂进行对象实例化的操作
     */
    @Test
    public  void test03() {
//        1、获取容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//        通过bean的id进行对象获取
        AccountService accountService= (AccountService) ac.getBean("accountService2");
        System.out.println(accountService);

    }


    @Test
    public  void test02() {
//        1、获取容器
        ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//        通过bean的id进行对象获取
        AccountService accountService= (AccountService) ac.getBean("accountService");
        AccountService accountService2= (AccountService) ac.getBean("accountService");
        System.out.println("两个对象是否指向同一个内存地址");
        System.out.println(accountService==accountService2);
//        调用close方法则容器关闭（销毁）
        ac.close();
    }
    @Test
    public  void test01() {
//        1、获取容器
//        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        ApplicationContext ac=new FileSystemXmlApplicationContext("D:\\beans.xml");
//        通过bean的id进行对象获取
        AccountService accountService= (AccountService) ac.getBean("accountService");
        System.out.println(accountService);
        AccountDao accountDao= (AccountDao) ac.getBean("accountDao");
        System.out.println(accountDao);


    }
}
